<template>
  <div>
    <!-- 添加用工人员 -->
    <el-dialog
      v-model="settlementConfigurationVisible"
      title="结算配置"
      width="70%"
    >
      <el-form
        ref="settlementConfigurationFormRef"
        :model="settlementConfigurationForm"
        label-width="auto"
        label-suffix=":"
      >
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="结算通道">
              <el-radio-group
                v-model="classAliasCheck"
                class="ml-4"
                @change="classAliasChange"
              >
                <el-radio
                  size="default"
                  v-for="(item,i) in classAlias"
                  :label="item.conf_group"
                  :value="item.conf_group"
                  :key="item.conf_group"
                >{{ item.conf_group_name }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            v-for="item in settlementConfigurationForm"
          >
            <el-form-item :label="item.conf_remark">
              <el-input
                v-model="item.conf_value"
                clearable
                :placeholder="'请输入'+item.conf_remark"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24"  v-if="classAliasCheck == 'settlement.zhifubao'">
            <div style="
           text-align: right;
           color: #349dff;
            ">
              <div>签约信息</div>
              <div>协议编号：{{ getSignInfoData.agreementNo||'' }}</div>
              <div>支付宝登录名：{{ getSignInfoData.alipayLogonId||'' }}</div>
              <div>签约时间：{{ getSignInfoData.validTime||'' }}</div>
              <div>生效时间：{{ getSignInfoData.signTime||'' }}</div>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="settlementConfigurationVisible = false">关 闭</el-button>
          <el-button
            type="primary"
            :loading="submitLoading"
            @click="addUwbFun"
          >
            确 定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="settlementConfiguration" lang="ts">
import api from '@/api/service/addService';
import { AddServiceVO, SignZhifubao, UnitConfDetail, UnitConfGroup } from '@/api/service/addService/types';

const classAlias = ref<UnitConfGroup>([]);
const classAliasCheck = ref<string | number | boolean>('');
const unit_id = ref<string | number>('');
const sydName = ref('');
const settlementConfigurationVisible = ref(false);
const settlementConfigurationFormRef = ref<ElFormInstance>();
const settlementConfigurationForm = ref<UnitConfDetail[]>();
const submitLoading = ref(false)

const settlementConfigurationVisibleFun = async (row: AddServiceVO) => {
  settlementConfigurationVisible.value = true;
  unit_id.value = row.id;
  sydName.value = row.gsmc;
  const res = await api.getConfGroupByUnit(unit_id.value);
  for(let i = 0 ; i < res.data.length; i++){
      if(res.data[i].is_enable == 1){
          classAliasCheck.value = res.data[i].conf_group;
      }
  }
  if(!classAliasCheck.value){
      classAliasCheck.value = res.data[0].conf_group;
  }

  await getConfDetailByUnitFun();
  classAlias.value = res.data;
  if(classAliasCheck.value  == 'settlement.zhifubao'){
    await getSignInfoFun();
  }

};
const getConfDetailByUnitFun = async () => {
  const ress = await api.getConfDetailByUnit(unit_id.value, classAliasCheck.value);
  settlementConfigurationForm.value = ress.data;
};

const getSignInfoData = ref<SignZhifubao>([]);
const getSignInfoFun = async () => {
  const res = await api.getSignInfo(unit_id.value);
  getSignInfoData.value = res.data;
};

const classAliasChange = async (value: string | number | boolean) => {
  console.log(value);
  classAliasCheck.value = value;
  await getConfDetailByUnitFun();
};

const addUwbFun = async () => {
  settlementConfigurationFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      settlementConfigurationForm.value?.forEach((e) => {
        e.conf_unit = unit_id.value;
      });
      submitLoading.value = true;
      const res = await api.editConfDetail({
        confs: JSON.stringify(settlementConfigurationForm.value)
      });
      const ress = await api.creatXNnum(unit_id.value,sydName.value);
      settlementConfigurationVisible.value = false;
      ElMessage.success('添加成功！');
      submitLoading.value = false;
    }
  });
};

defineExpose({
  settlementConfigurationVisibleFun
});
</script>

<style lang="scss" scoped>
</style>
